package com.birth.memo;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.birth.memo.service.BootCompletedReceiver;
import com.birth.memo.ui.ViewCharacter;
import com.birth.memo.ui.ViewConfig;
import com.birth.memo.ui.ViewHelp;
import com.birth.memo.ui.ViewMemoList;
import com.birth.memo.ui.ViewSmsList;

public class MainActivity extends Activity {
GridView mGrid;

	
	private int[] functionDescResources;
	
	@SuppressWarnings("unchecked")
	private Class[] views;

	private int[] funcIcons;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setTitle(R.string.app_name);
		this.setContentView(R.layout.main);
		
		
		mGrid = (GridView) this.findViewById(R.id.gridview);
		functionDescResources = new int[]{R.string.sys_function1,R.string.sys_function2,R.string.sys_function3,R.string.sys_function4,R.string.sys_function5};
		
		views = new Class[]{ViewMemoList.class,ViewCharacter.class,ViewSmsList.class,ViewConfig.class,ViewHelp.class};
		funcIcons = new int[]{R.drawable.func,R.drawable.func,R.drawable.func,R.drawable.func,R.drawable.func};

		ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < functionDescResources.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ItemImage", funcIcons[i]);// 添加图像资源的ID
			map.put("ItemText", getResources().getString(functionDescResources[i]));// 按序号做ItemText
			lstImageItem.add(map);
		}

		// 生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
		SimpleAdapter saImageItems = new SimpleAdapter(this, 
				lstImageItem,// 数据来源
				R.layout.grid_item,// item的XML实现
				
				// 动态数组与ImageItem对应的子项
				new String[] { "ItemImage", "ItemText" },

				// ImageItem的XML文件里面的一个ImageView,两个TextView ID
				new int[] { R.id.ItemImage, R.id.ItemText });

		mGrid.setAdapter(saImageItems);
		
		mGrid.setOnItemClickListener(new ItemClickListener());
		
		if(!BootCompletedReceiver.isRunning){
			BootCompletedReceiver.checkBirthday(getApplicationContext());
		}
			
					
	}
	
	class ItemClickListener implements OnItemClickListener {		
		@Override
		public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {				
			Intent intent = new Intent(MainActivity.this,views[position]);			
			MainActivity.this.startActivity(intent);
		}  
	}
}